package com.corn.turorial.spring.config;

import com.corn.turorial.spring.servlet.MyCustomerFilter;
import com.corn.turorial.spring.servlet.MyCustomerListener;
import com.corn.turorial.spring.servlet.MyCustomerServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Servlet;
import javax.servlet.ServletContextListener;
import java.util.Arrays;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2021/2/8 10:31
 */

@Configuration
public class MyServletConfiguration {

    @Bean
    public ServletRegistrationBean<Servlet> myServlet() {
        ServletRegistrationBean bean = new ServletRegistrationBean();
        bean.setServlet(new MyCustomerServlet());
        bean.setUrlMappings(Arrays.asList("/my/Servlet"));
        return bean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filter = new FilterRegistrationBean();
        filter.setFilter(new MyCustomerFilter());
        filter.setUrlPatterns(Arrays.asList("/my/*"));
        return filter;
    }

    @Bean
    public ServletListenerRegistrationBean myListener() {
        ServletListenerRegistrationBean<ServletContextListener> listener = new ServletListenerRegistrationBean<>();
        listener.setListener(new MyCustomerListener());
        return listener;
    }

}
